﻿#pragma once
#include "OCMain/FileUtil.h"
#include "OCMain/Log.h"
#include "rapidjson/writer.h"
#include "rapidjson/stringbuffer.h"
#include "OCGrib/GribReader.h"
#include "OCVolume/VolumeEncoder.h"
#include "OCVolume/VolumeMesh.h"

using namespace OCPP;
using namespace OCPP::Volume;

class MarchingCubesWriter : public OCPP::VolumeWriter
{
public:
	String name;
	MarchingCubesWriter(String _name) {
		name = _name;
	}
	virtual String generateOutputName(const VolumeGridData& ncGridData, const VolumeOptions& ncOptions)
	{
		return StringUtil::replaceAll(name, ".nc", "");
	}

	virtual bool write(const VolumeGridData& ncGridData, const VolumeOptions& ncOptions)
	{
		FloatVector isoValues = { 15.0f,20.0f };
		String fileName = generateOutputName(ncGridData, ncOptions);
		String path = getOutputPath();
		VolumeMesh volumeMesh;
		volumeMesh.convert(joinPath(path, fileName), isoValues, 25, ncGridData);
		return true;
	}
};

void ExtractISO()
{
	String output = "\\\\10.104.207.155\\Cesium\\Assets\\2024\\SampleData\\ISO";
	String dir = U2A("E:\\volume\\data2024\\18");

	StringMap globalOptions;
	globalOptions[VolumeOptions::Config::NameWriter] = VolumeOptions::Config::ValueD3Writer;
	globalOptions[VolumeOptions::Config::NameGZipCompress] = "yes";
	globalOptions[VolumeOptions::Config::NameMakeDirForFile] = "yes";
	globalOptions[VolumeOptions::Config::NameGenerateNormal] = "no";
	globalOptions[VolumeOptions::Config::NameClipRange] = "70 0 140 60";

	String ext = "grib";
	StringVector files = FileUtil::findFiles(dir);
	for (const auto& f : files)
	{
		if (!StringUtil::endsWith(f, "." + ext)) continue;

		String fileName = joinPath(dir, f);
		OCPP_LOG_INFO << fileName << "\n";

		VolumeComputeList computeList;
		computeList.push_back({ fileName ,"u-component_of_wind_isobaric" });
		computeList.push_back({ fileName ,"v-component_of_wind_isobaric" });

		auto* myWriter = new MarchingCubesWriter(StringUtil::replaceAll(f, "." + ext, ""));
		myWriter->setOutputPath(output);

		VolumeOptions vOptions(globalOptions);
		vOptions.setWriter(myWriter);

		GribReader::extract(computeList, std::make_shared<NcAlgorithmVecLength>(), vOptions
			, -1, -1);

		//break;
	}

	OCPP_LOG_INFO << dir << "\n";
}
